-- EXEC SP_UPDATE_COST_FROM_PKG_WAFER_COST_TABLE 2011,2
ALTER PROC SP_UPDATE_COST_FROM_PKG_WAFER_COST_TABLE ( @Year INT, @Quarter INT) AS
BEGIN

	DECLARE @ProcessStep_ID INT
	DECLARE @WaferCost	NUMERIC(18,4)
	DECLARE @BGCost		NUMERIC(18,4)
	DECLARE @PkgCost	NUMERIC(18,4)
	DECLARE @testTimeInSec	NUMERIC(18,4)
	DECLARE @testCostPerSec	NUMERIC(18,4)
	DECLARE @TRCost 	NUMERIC(18,4)
	DECLARE @TRCostY	NUMERIC(18,4)
	DECLARE @TRCostT	NUMERIC(18,4)
	DECLARE @bulkCost	NUMERIC(18,4)
	DECLARE @YieldAssy	NUMERIC(18,4)
	DECLARE @YieldTest	NUMERIC(18,4)
	DECLARE @Yield		NUMERIC(18,4)
	DECLARE @Raw_Wafer_Cost		NUMERIC(18,4)
	DECLARE @ZRO_EXP2_Cost		NUMERIC(18,4) 
	DECLARE @EPI_VT_Cost		NUMERIC(18,4)
	DECLARE @VN_EXP_Cost		NUMERIC(18,4)
	DECLARE @Epi_Seal_Cost		NUMERIC(18,4)
	DECLARE @Pre_Grind_Cost		NUMERIC(18,4)
	DECLARE @Sort_Cost			NUMERIC(18,4)
	DECLARE @Sort_Yield			NUMERIC(18,4)
	DECLARE @BG_Yield			NUMERIC(18,4)
	DECLARE @Mix_Match_Yield			NUMERIC(18,4)
	DECLARE @GDW		INT
	DECLARE @Total_Wafer_Cost	NUMERIC(18,4)
	DECLARE @Cumm_Cost_Per_Die	NUMERIC(18,4)
	DECLARE @CMOS_Cumm_Cost_Per_Die NUMERIC(18,4)
	DECLARE @FG_Cumm_Cost_Per_Die NUMERIC(18,4)
	DECLARE @MEMS_Cumm_Cost_Per_Die NUMERIC(18,4)
	DECLARE @Process_Type VARCHAR(50)
	DECLARE @Process_parameter VARCHAR(50)
	DECLARE @ROWCOUNT	INT
	DECLARE @BG_ProcessStep_ID INT
	DECLARE @FAB_ProcessStep_ID INT
	DECLARE @MEMS_ProcessStep_ID INT
	DECLARE @DB_ProcessStep_ID INT
	DECLARE @ASSM_ProcessStep_ID INT
	DECLARE @PRETEST_ProcessStep_ID INT
	DECLARE @FT_ProcessStep_ID INT
	DECLARE @TR_ProcessStep_ID INT
	DECLARE @FG_ProcessStep_ID INT
	DECLARE @FG_Part_Number VARCHAR(50)
	DECLARE @TR_Part VARCHAR(50)
	DECLARE @LAST_CHAR CHAR
	DECLARE @ProcessStep_Cursor CURSOR
	DECLARE @PART_NO_END_CHAR VARCHAR(2)
		
	/*DECLARE COST_CURSOR CURSOR FOR SELECT [Processstep_Id],[Wafer Cost],[BackGrind Cost],[Pkg Cost],
			[Test Time InSec],[Test Cost PerSec],[TR CostY],[TR CostT],
			[Bulk],[Yield_Assy],[Yield_Test],[Yield],[Raw_Wafer_Cost],[ZRO_EXP2_Cost],[EPI_VT_Cost],
			[VN_EXP_Cost],[Epi_Seal_Cost],[Pre_Grind_Cost],[Back_Grind_Yield],[Sort_Cost],
			[Sort_Yield],[Mix_Match_Yield],[Cumm_Cost_Per_Die]
			FROM [Wafer_Pkg_Cost_Table] WHERE Quarter = @Quarter AND Year = @Year*/
	DECLARE COST_CURSOR CURSOR FOR SELECT WPCT.[Processstep_Id],[Wafer Cost],[BackGrind Cost],[Pkg Cost],
			[Test Time InSec],[Test Cost PerSec],[TR CostY],[TR CostT],
			[Bulk],[Yield_Assy],[Yield_Test],[Yield],[Raw_Wafer_Cost],[ZRO_EXP2_Cost],[EPI_VT_Cost],
			[VN_EXP_Cost],[Epi_Seal_Cost],[Pre_Grind_Cost],[Back_Grind_Yield],[Sort_Cost],
			[Sort_Yield],[Mix_Match_Yield],[Cumm_Cost_Per_Die]
			FROM [Wafer_Pkg_Cost_Table] WPCT, Process_Step PS,  Process_Types_Table PTT WHERE 
			WPCT.ProcessStep_Id = PS.ProcessStep_ID and PS.PS_Type = PTT.Process_Type AND Quarter = @Quarter AND Year = @Year
			ORDER BY SORTING_ORDER

	OPEN COST_CURSOR
	FETCH NEXT FROM COST_CURSOR INTO @ProcessStep_ID, @WaferCost, @BGCost, @PkgCost, @testTimeInSec, @testCostPerSec, @TRCostY, @TRCostT,
	@bulkCost, @YieldAssy, @YieldTest, @Yield, @Raw_Wafer_Cost, @ZRO_EXP2_Cost, @EPI_VT_Cost, @VN_EXP_Cost, @Epi_Seal_Cost, @Pre_Grind_Cost,
	@BG_Yield, @Sort_Cost, @Sort_Yield, @Mix_Match_Yield, @Cumm_Cost_Per_Die
			
	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @Process_Type = null
		SET @Process_parameter = null
		SELECT @Process_Type = PS_TYPE_NAME FROM Process_Step WHERE ProcessStep_ID = @PROCESSSTEP_ID
		SELECT @Process_parameter = Parameter_Value FROM PROCESS_PARAMETER WHERE ProcessStep_Id = @PROCESSSTEP_ID AND Parameter_Key = 'IS_MEMS_WAFER'
		IF @Process_parameter IS NOT NULL AND @Process_parameter = 'TRUE'
		BEGIN
			SET @Process_Type = 'MEMS'
		END

		IF( @Process_Type = 'Die Bank' )
		BEGIN
			SELECT @FAB_ProcessStep_ID = ProcessStep_ID FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'FAB'
			-- Updating the Fab Cost
			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @WaferCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @WaferCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END		
			-- Back Grind Cost
			SELECT @BG_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Back Grind'
			IF @BG_ProcessStep_ID IS NOT NULL AND @BG_ProcessStep_ID > 0
			BEGIN
				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @BG_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @BG_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @BG_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @BG_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @BG_ProcessStep_ID, @WaferCost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @BG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @BG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @BG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @WaferCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @BG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END					
			END

			-- Die Bank
			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @PROCESSSTEP_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END	
			END		
		END
		ELSE IF @Process_Type = 'ASSEMBLY' 
		BEGIN
			SELECT @DB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Die Bank'
			SELECT @CMOS_Cumm_Cost_Per_Die = ISNULL( Cumm_Cost_Per_Die, 0.0) FROM Wafer_Pkg_Cost_Table WHERE ProcessStep_ID = @DB_ProcessStep_ID AND Quarter = @Quarter AND Year = @Year

			SELECT @MEMS_ProcessStep_ID = bom_processstep_id FROM Assembly_BOM_Setup_Table WHERE assembly_processstep_id = @PROCESSSTEP_ID
			--SELECT @MEMS_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @MEMS_ProcessStep_ID, 0 ) WHERE PS_TYPE = 'FAB'
			SELECT @MEMS_Cumm_Cost_Per_Die = ISNULL( Cumm_Cost_Per_Die, 0.0) FROM Wafer_Pkg_Cost_Table WHERE ProcessStep_ID = @MEMS_ProcessStep_ID AND Quarter = @Quarter AND Year = @Year

			--SELECT @DB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Die Bank'
			SET @CUMM_Cost_Per_Die = ISNULL( @CMOS_Cumm_Cost_Per_Die, 0.0 ) + ISNULL( @MEMS_Cumm_Cost_Per_Die, 0.0 )
			


			-- Assembly
			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @PROCESSSTEP_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @CUMM_Cost_Per_Die, @PkgCost, @YieldAssy, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Q2_Yield, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @CUMM_Cost_Per_Die, @PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost,Q3_Fixed_Over_head_Cost, Q3_Yield, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @CUMM_Cost_Per_Die,@PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost,Q4_Fixed_Over_head_Cost, Q4_Yield, Fin_Year, Timestamp) 
						VALUES ( @PROCESSSTEP_ID, @CUMM_Cost_Per_Die,@PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost= @PkgCost, Q1_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost= @PkgCost, Q2_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost= @PkgCost, Q3_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost= @PkgCost, Q4_Yield = @YieldAssy,Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END	
			END		
			
			SELECT DISTINCT @PRETEST_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'Pre Test'

			SET @CUMM_Cost_Per_Die = @CUMM_Cost_Per_Die/(ISNULL(@YieldAssy, 100.00)/100) + ISNULL(@PkgCost, 0.0)
			-- Pre Test
			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @PRETEST_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost,Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Q3_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost= @PkgCost,Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END		
			
			-- FT Process
			SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'FT'
			OPEN @ProcessStep_Cursor
			FETCH NEXT FROM @ProcessStep_Cursor INTO @FT_ProcessStep_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FT_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Q2_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost,Q3_Fixed_Over_head_Cost, Q3_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Q4_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q1_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q2_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q3_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q4_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				FETCH NEXT FROM @ProcessStep_Cursor INTO @FT_ProcessStep_ID
			END
			CLOSE @ProcessStep_Cursor
			DEALLOCATE @ProcessStep_Cursor
			--DECLARE @PART_NO_END_CHAR VARCHAR(2)
			
			-- Tape & Reel Process 
			SET @CUMM_Cost_Per_Die = (@CUMM_Cost_Per_Die + ( ISNULL(@testTimeInSec, 1.0)*ISNULL(@testCostPerSec, 0.0) ) )/(ISNULL(@YieldTest, 100.00)/100.00)

			SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'Tape & Reel'
			OPEN @ProcessStep_Cursor
			FETCH NEXT FROM @ProcessStep_Cursor INTO @TR_ProcessStep_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				SELECT @TR_PART = PART_NUMBER FROM PRODUCT WHERE PROCESSSTEP_ID = @TR_ProcessStep_ID
				SELECT @PART_NO_END_CHAR = SubString( @TR_PART , LEN(@TR_PART), 1)
				IF @PART_NO_END_CHAR = 'Y' 
				BEGIN
					SET @TRCost = @TRCostY
				END
				ELSE IF @PART_NO_END_CHAR = 'T'
				BEGIN
					SET @TRCost = @TRCostT
				END
				ELSE
				BEGIN
					SET @TRCost = @bulkCost	
				END	
				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @TR_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost,Q2_Fixed_Over_head_Cost, Q2_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Q3_Fixed_Over_head_Cost, Q3_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Q4_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die,  @TRCost,@Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost=@TRCost,Q1_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost=@TRCost,Q2_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost=@TRCost,Q3_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost=@TRCost,Q4_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				FETCH NEXT FROM @ProcessStep_Cursor INTO @TR_ProcessStep_ID
			END
			CLOSE @ProcessStep_Cursor
			DEALLOCATE @ProcessStep_Cursor

			-- Finished Goods
			SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'FG'
			OPEN @ProcessStep_Cursor
			FETCH NEXT FROM @ProcessStep_Cursor INTO @FG_ProcessStep_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				SELECT @FG_Part_Number = Part_Number FROM Product WHERE ProcessStep_ID = @FG_ProcessStep_ID
				
				SELECT @LAST_CHAR = SubString( @FG_Part_Number , LEN(@FG_Part_Number), 1)
				IF @LAST_CHAR = 'T'
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@TRCostT, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END
				ELSE IF @LAST_CHAR = 'Y'
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@TRCostY, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END
				ELSE
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@bulkCost, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END

				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FG_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @FG_ProcessStep_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @FG_ProcessStep_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @FG_ProcessStep_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @FG_ProcessStep_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FG_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				FETCH NEXT FROM @ProcessStep_Cursor INTO @FG_ProcessStep_ID
			END
			CLOSE @ProcessStep_Cursor
			DEALLOCATE @ProcessStep_Cursor
 			SET @DB_ProcessStep_ID = 0
			SET @MEMS_ProcessStep_ID = 0
			SET @PRETEST_ProcessStep_ID = 0
		END

		ELSE IF @Process_Type = 'Finished Goods' 
		BEGIN
			SELECT @DB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Die Bank'
			SELECT @CMOS_Cumm_Cost_Per_Die = ISNULL( Cumm_Cost_Per_Die, 0.0) FROM Wafer_Pkg_Cost_Table WHERE ProcessStep_ID = @DB_ProcessStep_ID AND Quarter = @Quarter AND Year = @Year

			SELECT @MEMS_ProcessStep_ID = bom_processstep_id FROM Assembly_BOM_Setup_Table WHERE assembly_processstep_id = @PROCESSSTEP_ID
			--SELECT @MEMS_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @MEMS_ProcessStep_ID, 0 ) WHERE PS_TYPE = 'FAB'
			SELECT @MEMS_Cumm_Cost_Per_Die = ISNULL( Cumm_Cost_Per_Die, 0.0) FROM Wafer_Pkg_Cost_Table WHERE ProcessStep_ID = @MEMS_ProcessStep_ID AND Quarter = @Quarter AND Year = @Year

			--SELECT @DB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Die Bank'
			SET @CUMM_Cost_Per_Die = ISNULL( @CMOS_Cumm_Cost_Per_Die, 0.0 ) + ISNULL( @MEMS_Cumm_Cost_Per_Die, 0.0 )
			
			SELECT @ASSM_ProcessStep_ID = ProcessStep_ID FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Assembly'
			-- Assembly NO NEED TO CALCULATING THE COST FOR ASSEMBLY WHEN THE SETUP IS FOR FG PART NUMBER
			/*SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @ASSM_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
						VALUES ( @ASSM_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost, @YieldAssy, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Q2_Yield, Fin_Year, Timestamp) 
						VALUES ( @ASSM_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost,Q3_Fixed_Over_head_Cost, Q3_Yield, Fin_Year, Timestamp) 
						VALUES ( @ASSM_ProcessStep_ID, @CUMM_Cost_Per_Die,@PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost,Q4_Fixed_Over_head_Cost, Q4_Yield, Fin_Year, Timestamp) 
						VALUES ( @ASSM_ProcessStep_ID, @CUMM_Cost_Per_Die,@PkgCost,  @YieldAssy,'01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost= @PkgCost, Q1_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost= @PkgCost, Q2_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost= @PkgCost, Q3_Yield = @YieldAssy, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost= @PkgCost, Q4_Yield = @YieldAssy,Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
				END	
			END	*/
			
			SELECT DISTINCT @PRETEST_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'Pre Test'

			SET @CUMM_Cost_Per_Die = @CUMM_Cost_Per_Die/(ISNULL(@YieldAssy, 100.00)/100) + ISNULL(@PkgCost, 0.0)

			-- Pre Test NEED NOT UPDATE FOR FG ALSO.
			/*SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @PRETEST_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost,Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Q3_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Fin_Year, Timestamp) 
						VALUES ( @PRETEST_ProcessStep_ID, @CUMM_Cost_Per_Die, @PkgCost,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost= @PkgCost,Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost= @PkgCost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PRETEST_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END	*/
			
			-- FT Process
 
			SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'FT'
			OPEN @ProcessStep_Cursor
			FETCH NEXT FROM @ProcessStep_Cursor INTO @FT_ProcessStep_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FT_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q2_Fixed_Over_head_Cost, Q2_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost,Q3_Fixed_Over_head_Cost, Q3_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest,  '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Q4_Yield,Fin_Year, Timestamp) 
							VALUES ( @FT_ProcessStep_ID, @CUMM_Cost_Per_Die, (ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), @YieldTest, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q1_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q2_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q3_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost=(ISNULL(@testTimeInSec,1.0)*ISNULl(@testCostPerSec,0.0)), Q4_Yield = @YieldTest, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FT_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				FETCH NEXT FROM @ProcessStep_Cursor INTO @FT_ProcessStep_ID
			END
			CLOSE @ProcessStep_Cursor
			DEALLOCATE @ProcessStep_Cursor
			
			
			-- Tape & Reel Process 
			SET @CUMM_Cost_Per_Die = (@CUMM_Cost_Per_Die + ( ISNULL(@testTimeInSec, 1.0)*ISNULL(@testCostPerSec, 0.0) ) )/(ISNULL(@YieldTest, 100.00)/100.00)

			--SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Tape & Reel'
			--OPEN @ProcessStep_Cursor
			--FETCH NEXT FROM @ProcessStep_Cursor INTO @TR_ProcessStep_ID
			--WHILE @@FETCH_STATUS = 0
			--BEGIN
				SELECT TOP 1 @TR_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Tape & Reel'
				SELECT @TR_PART = PART_NUMBER FROM PRODUCT WHERE PROCESSSTEP_ID = @TR_ProcessStep_ID
				SELECT @PART_NO_END_CHAR = SubString( @TR_PART , LEN(@TR_PART), 1)
				IF @PART_NO_END_CHAR = 'Y' 
				BEGIN
					SET @TRCost = @TRCostY
				END
				ELSE IF @PART_NO_END_CHAR = 'T'
				BEGIN
					SET @TRCost = @TRCostT
				END
				ELSE
				BEGIN
					SET @TRCost = @bulkCost	
				END	
				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @TR_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q1_Fixed_Over_head_Cost, Q1_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost,Q2_Fixed_Over_head_Cost, Q2_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Q3_Fixed_Over_head_Cost, Q3_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die, @TRCost, @Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Q4_Fixed_Over_head_Cost, Q4_Yield, Fin_Year, Timestamp) 
							VALUES ( @TR_ProcessStep_ID, @CUMM_Cost_Per_Die,  @TRCost,@Yield, '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Q1_Fixed_Over_head_Cost=@TRCost,Q1_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Q2_Fixed_Over_head_Cost=@TRCost,Q2_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Q3_Fixed_Over_head_Cost=@TRCost,Q3_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Q4_Fixed_Over_head_Cost=@TRCost,Q4_Yield = @Yield, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @TR_ProcessStep_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				--FETCH NEXT FROM @ProcessStep_Cursor INTO @TR_ProcessStep_ID
			--END
			--CLOSE @ProcessStep_Cursor
			--DEALLOCATE @ProcessStep_Cursor

			-- Finished Goods
			--SET @ProcessStep_Cursor = CURSOR FOR SELECT DISTINCT ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = 'FG'
			--OPEN @ProcessStep_Cursor
			--FETCH NEXT FROM @ProcessStep_Cursor INTO @FG_ProcessStep_ID
			--WHILE @@FETCH_STATUS = 0
			--BEGIN
				SELECT @FG_Part_Number = Part_Number FROM Product WHERE ProcessStep_ID = @PROCESSSTEP_ID
				
				SELECT @LAST_CHAR = SubString( @FG_Part_Number , LEN(@FG_Part_Number), 1)
				IF @LAST_CHAR = 'T'
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@TRCostT, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END
				ELSE IF @LAST_CHAR = 'Y'
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@TRCostY, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END
				ELSE
				BEGIN
					SET @FG_Cumm_Cost_Per_Die = ( @CUMM_Cost_Per_Die + ISNULL(@bulkCost, 0.0) )/(ISNULL( @Yield, 100.00)/100.00)
				END

				SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @PROCESSSTEP_ID AND YEAR(FIN_YEAR) = @Year
				IF ( @ROWCOUNT = 0 )
				BEGIN
					IF @Quarter = 1
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @PROCESSSTEP_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 2
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @PROCESSSTEP_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 3
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @PROCESSSTEP_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END
					ELSE IF @Quarter = 4
					BEGIN
						INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
							VALUES ( @PROCESSSTEP_ID, @FG_Cumm_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
					END	
				END
				ELSE
				BEGIN
					IF @Quarter = 1
					BEGIN
						UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 2
					BEGIN
						UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 3
					BEGIN
						UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
					END
					ELSE IF @Quarter = 4
					BEGIN
						UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @FG_Cumm_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @PROCESSSTEP_ID AND YEAR(FIN_Year) = @Year
					END	
				END		
				--FETCH NEXT FROM @ProcessStep_Cursor INTO @FG_ProcessStep_ID
			--END
			----CLOSE @ProcessStep_Cursor
			---EALLOCATE @ProcessStep_Cursor
			SET @DB_ProcessStep_ID = 0
			SET @MEMS_ProcessStep_ID = 0
			SET @PRETEST_ProcessStep_ID = 0
			
		END
		ELSE IF ( @Process_Type = 'MEMS' ) 
		BEGIN
			-- Raw Wafer
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Raw Wafer'
			--SET @CUMM_Cost_Per_Die = ISNULL( @CMOS_Cumm_Cost_Per_Die, 0.0 ) + ISNULL( @MEMS_Cumm_Cost_Per_Die, 0.0 )

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- ZRO_EXP2
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'ZRO_EXP2'
			
			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @Raw_Wafer_Cost,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @Raw_Wafer_Cost, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- epi vt
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Epi VT'
			SET @CUMM_Cost_Per_Die = ISNULL( @Raw_Wafer_Cost, 0.0) + ISNULL( @ZRO_EXP2_Cost, 0.0) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- vn_exp
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'VN_EXP'
			SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0) + ISNULL( @EPI_VT_Cost, 0.0) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- ePI sEAL
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Epi Seal'
			SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0.0) + ISNULL( @VN_EXP_Cost, 0.0) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- FAB
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Fab'
	
			SELECT @GDW = ISNULL( PARAMETER_VALUE, 1) FROM Process_Parameter WHERE Parameter_Key = 'FIN_GDW' AND ProcessStep_Id = @FAB_ProcessStep_ID 		
			IF( @GDW IS NULL OR @GDW < 1  )
			BEGIN
				SELECT @GDW = ISNULL( PARAMETER_VALUE, 1) FROM Process_Parameter WHERE Parameter_Key = 'Gross Die/Wafer' AND ProcessStep_Id = @FAB_ProcessStep_ID 
			END			

			SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0.0 ) + ISNULL( @Epi_Seal_Cost, 0.0 ) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- PRE GRIND
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'PRE_GRIND'

			SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0.0 ) + ISNULL( @WaferCost, 0.0 ) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- SPARSE PROBE
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Sparse Probe'
			
			--SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0.0 ) + ISNULL( @WaferCost, 0.0 ) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- BACK GRIND
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Back Grind'

			SET @CUMM_Cost_Per_Die = ISNULL( @CUMM_Cost_Per_Die, 0.0 ) + ISNULL( @Pre_Grind_Cost, 0.0 ) 

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END
			-- sort
			SELECT @FAB_ProcessStep_ID = ProcessStep_Id FROM FN_GET_PROCESS_FLOWS_BY_ProcessStep_ID ( @PROCESSSTEP_ID, 0 ) WHERE PS_TYPE = 'Sort'

			SET @CUMM_Cost_Per_Die = (ISNULL( @CUMM_Cost_Per_Die, 0.0) + ISNULL( @BGCost, 0.0) )/(ISNULL(@BG_Yield, 100.00)/100.00)

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END

			-- Die Bank
			SET @FAB_ProcessStep_ID = @PROCESSSTEP_ID

			SET @CUMM_Cost_Per_Die = ((ISNULL( @CUMM_Cost_Per_Die, 0) + ISNULL( @Sort_Cost, 0) )/(ISNULL(@Sort_Yield, 100.00)/100.00) )
			SET @CUMM_Cost_Per_Die = ( ISNULL( @CUMM_Cost_Per_Die, 0 )/(ISNULL(@Mix_Match_Yield, 100.00)/100.00) )/@GDW

			SELECT @ROWCOUNT = COUNT(*) FROM Update_View_Cost_Table where ProcessStep_ID = @FAB_ProcessStep_ID AND YEAR(FIN_YEAR) = @Year
			IF ( @ROWCOUNT = 0 )
			BEGIN
				IF @Quarter = 1
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 2
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 3
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END
				ELSE IF @Quarter = 4
				BEGIN
					INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp) 
						VALUES ( @FAB_ProcessStep_ID, @CUMM_Cost_Per_Die,   '01/01/'+CONVERT(char(4),@Year), CURRENT_TIMESTAMP  )
				END	
			END
			ELSE
			BEGIN
				IF @Quarter = 1
				BEGIN
					UPDATE Update_View_Cost_Table SET Q1_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 2
				BEGIN
					UPDATE Update_View_Cost_Table SET Q2_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 3
				BEGIN
					UPDATE Update_View_Cost_Table SET Q3_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END
				ELSE IF @Quarter = 4
				BEGIN
					UPDATE Update_View_Cost_Table SET Q4_Standard_Cost = @CUMM_Cost_Per_Die, Timestamp = CURRENT_TIMESTAMP WHERE ProcessStep_id = @FAB_ProcessStep_ID AND YEAR(FIN_Year) = @Year
				END	
			END
		END

		FETCH NEXT FROM COST_CURSOR INTO @ProcessStep_ID, @WaferCost, @BGCost, @PkgCost, @testTimeInSec, @testCostPerSec, @TRCostY, @TRCostT,
		@bulkCost, @YieldAssy, @YieldTest, @Yield, @Raw_Wafer_Cost, @ZRO_EXP2_Cost, @EPI_VT_Cost, @VN_EXP_Cost, @Epi_Seal_Cost, @Pre_Grind_Cost,
		@BG_Yield, @Sort_Cost, @Sort_Yield, @Mix_Match_Yield, @Cumm_Cost_Per_Die
	END
	CLOSE COST_CURSOR
	DEALLOCATE COST_CURSOR
END


